package day9;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileSenderThread2 extends Thread {
    private Socket s;
    public FileSenderThread2(Socket s){
        this.s=s;
    }
    public static final String FOLDER="E:\\java_workspace\\hbuilderproj\\java75\\java75pre\\images";
    @Override
    public void run() {
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();// 换行符
            System.out.println(s+"连上了，准备接受文件。。。");
            FileInputStream fin=new FileInputStream(new File(FOLDER,fileName));
            byte[] b=new byte[fin.available()];
            int len=fin.read(b);
            out.write(b,0,len);
            out.flush();
            System.out.println("文件发送给"+s+"完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(s!=null) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
